﻿@using System.Net.Http.Json
@using MudBlazor.Examples.Data.Models
@namespace MudBlazor.Docs.Examples
@inject HttpClient httpClient

<MudDataGrid @ref="_dataGrid" Items="@Elements" ReadOnly="@_isReadOnly" EditMode="@DataGridEditMode.Cell" ExpandSingleRow="_expandSingleRow">
    <Columns>
        <HierarchyColumn T="Element" ButtonDisabledFunc="@(x => x.Sign == "He")" InitiallyExpandedFunc="@(x => x.Sign == "Li")" EnableHeaderToggle="@_enableHeaderToggle" />
        <PropertyColumn Property="x => x.Number" Title="Nr" />
        <PropertyColumn Property="x => x.Sign" />
        <PropertyColumn Property="x => x.Name" />
        <PropertyColumn Property="x => x.Position" />
        <PropertyColumn Property="x => x.Molar" Title="Molar mass" />
    </Columns>
    <ChildRowContent>
        <MudCard>
            <MudCardHeader>
                <CardHeaderContent>
                    <MudText Typo="Typo.h6">@context.Item.Name</MudText>
                </CardHeaderContent>
            </MudCardHeader>
            <MudCardContent>
                <MudText>This element is number @context.Item.Number</MudText>
                <MudText>This element has a molar mass of @context.Item.Molar</MudText>
            </MudCardContent>
        </MudCard>
    </ChildRowContent>
    <PagerContent>
        <MudDataGridPager T="Element" />
    </PagerContent>
</MudDataGrid>

<div class="d-flex flex-wrap mt-4">
    <MudSwitch T="bool" @bind-Value="_isReadOnly" Color="@Color.Primary">Read Only</MudSwitch>
    <MudButton OnClick="@ExpandAll" Color="@Color.Primary">Expand all</MudButton>
    <MudButton OnClick="@CollapseAll" Color="@Color.Primary">Collapse all</MudButton>
    <MudSwitch @bind-Value="@_enableHeaderToggle" Label="Enable header toggle" Color="@Color.Primary" />
    <MudSwitch @bind-Value="@_expandSingleRow" Label="Expand single row" Color="@Color.Primary" />
</div>

@code {

    private bool _isReadOnly = true;
    private bool _expandSingleRow = false;
    private bool _enableHeaderToggle = false;
    private MudDataGrid<Element> _dataGrid = null!;
    private IEnumerable<Element> Elements = new List<Element>();

    protected override async Task OnInitializedAsync()
    {
        Elements = await httpClient.GetFromJsonAsync<List<Element>>("webapi/periodictable");
    }

    private Task ExpandAll()
    {
        return _dataGrid.ExpandAllHierarchy();
    }

    private Task CollapseAll()
    {
        return _dataGrid.CollapseAllHierarchy();
    }
}
